<template>
	<view>
		<u-sticky :enable="enable">
			<view class="steps">
				<u-steps :list="numList" :current="current" mode="number" active-color="white" un-active-color="white">
				</u-steps>
			</view>
		</u-sticky>
		<view style="padding-bottom:120rpx;">
			<view class="container">
				<view>
					<u-row>
						<u-col span="3">
							<u-button type="primary" size="mini" plain @click="addSpecifications">添加规格</u-button>
						</u-col>
						<u-col span="3">
							<u-button type="primary" size="mini" plain @click="addSku">添加SKU</u-button>
						</u-col>
					</u-row>
				</view>
			</view>
			<view class="goodsItem" v-for="(item,index) in goodsData" :key="index">
				<view class="goods">
					<view class="goods_img">
						<u-image :src="item.skuPicUrl" width="100%" height="100%"></u-image>
					</view>
					<view class="goods_detail">
						<view style="color: #000000;" class="ellipsis">
							<u-tag style="border: none;margin-right: 10rpx;" :text="item.skuTotalStocks>item.skuWarnUnit?'库存充足':'库存不充'" :type="item.skuTotalStocks>item.skuWarnUnit?'primary':'error'" mode="light" size="mini"/>
							<text>{{item.skuName}}</text>
						</view>
						<view>
							销售价：<text style="color: red;">¥{{item.skuSalePrice}}</text>｜
							<text style="color: #666;">成本价：<text>¥{{item.skuCostPrice}}</text></text>
						</view>
						<view>
							糖果抵扣率：{{item.skuSweetDeductRate}}%
						</view>
						<view>库存：<text>{{item.skuTotalStocks}}</text></view>
					</view>
				</view>
				<view>编码：{{item.skuCode}}</view>
				<view>规格：{{item.propertiesValue}}</view>
				<view style="display: flex;justify-content: flex-end;">
					<u-tag text="编辑" type="primary" plain @click="edit(item)"></u-tag>
					<u-tag text="删除" type="error" plain style="margin-left: 30rpx;" @click="delSku(item)"></u-tag>
				</view>
			</view>
			<view style="height: 80rpx;line-height: 80rpx;">
				<u-loadmore :status="status" />
			</view>
		</view>
		
		
		<view class="btnSum">
			<u-button type="primary" class="loginBtn" @click="back()">返回</u-button>
		</view>
		<u-modal v-model="show" content="此操作将永久删除该条SKU数据, 是否继续?" :zoom="false" show-cancel-button confirm-color="#1F6ECF" @confirm="confirm"></u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show:false,
				enable: true,
				current: 3,
				numList: [{
					name: '基本信息'
				}, {
					name: '商品图片'
				}, {
					name: '商品参数'
				}, {
					name: '规格库存'
				}],
				productId: '',
				page: 1,
				size: 10,
				total: 0,
				goodsData: [],
				id:'',
				status: 'loadmore',
			}
		},
		onShow() {
			this.enable = true;
		},
		onHide() {
			this.enable = false;
		},
		onLoad(option) {
			this.productId = option.productId;
		},
		onPullDownRefresh(){
			this.page = 1;
			this.goodsData = [];
			this.getData();
		},
		onReachBottom(){
			if (this.goodsData.length >= this.total) {
				this.status = 'nomore'
				return;
			}
			this.status = 'loading';
			this.page++;
			this.getData();
			setTimeout(() => {
				if (this.goodsData.length >= this.total) this.status = 'nomore';
				else this.status = 'loading';
			}, 2000)
		},
		mounted() {
			this.getData();
		},
		methods: {
			getData() {
				this.$u.api.skuList({
					product_id: this.productId,
					page: this.page,
					size: this.size
				}).then(res => {
					if (res.code == 0) {
						this.goodsData = [...this.goodsData,...res.data];
						this.total = res.extraInfo;
						if (res.extraInfo < 10) {
							this.status = 'nomore'
						}
					}
				})
			},
			addSpecifications(){
				uni.navigateTo({
					url:"/pages/line/shoping/addSpecifications?productId=" + this.productId
				})
			},
			addSku(){
				uni.navigateTo({
					url:"/pages/line/shoping/addSku?productId=" + this.productId
				})
			},
			delSku(item){
				this.id = item.id;
				this.show = true;
			},
			confirm(){
				this.$u.api.delSku({
					id:this.id
				}).then(res=>{
					if(res.code ==0){
						this.$u.toast('删除成功');
						this.getData();
					}else{
						this.$u.toast(res.msg);
					}
				})
			},
			edit(item){
				uni.navigateTo({
					url:"/pages/line/shoping/addSku?productId=" + this.productId+'&skuId='+item.id
				})
			},
			back(){
				var page = getCurrentPages()[0];
				console.log(page[0]);
				uni.switchTab({
				    url: '/pages/line/shoping/index',
					success:function(e) {
						var page = getCurrentPages()[0];
						if(page==undefined||page==null)return
						page.$vm.shuaxin();
					}
				});
			}
			
		}
	}
</script>

<style scoped lang="scss">
	.steps {
		background-color: #357cd4;
		padding: 30rpx;
	}

	.container {
		padding: 30rpx;
	}
	.goodsItem{
		padding:30rpx;
		border-bottom: 1px solid #f2f2f2;
	}
	.goods {
		display: flex;
		padding-bottom: 23rpx;
	
		.goods_img {
			width: 160rpx;
			height: 160rpx;
			border-radius: 10rpx;
			overflow: hidden;
			margin-right: 20rpx;
		}
	
		.goods_detail {
			font-size: 28rpx;
			color: #333333;
			flex: 1;
		}
		.ellipsis{
			overflow:hidden;
			text-overflow:ellipsis;
			display:-webkit-box; 
			-webkit-box-orient:vertical;
			-webkit-line-clamp:2;
		}
		
	}
	.btnSum {
		height: 50px;
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		background-color: white;
		border-top: 1px solid #f2f2f2;
		display: flex;
		align-items: center;
		z-index: 1;
		.loginBtn{
			width: 606rpx;
			background-color: #1F6ECF;
			border-radius: 43rpx;
			height: 70rpx;
			line-height: 70rpx;
			font-size: 32rpx;
			box-shadow: 0px 12px 16px 0px rgba(28, 100, 224, 0.32);
		}
	}
</style>
